import logging

from django.views import View
from django.http import JsonResponse
from datetime import datetime
from dwebsocket.decorators import accept_websocket
import time
from django.http import HttpResponseRedirect
from django.contrib.auth import authenticate, login

id_list = list()
errlog = logging.getLogger("Process")


# Create your views here.

class MainView(View):
    # @login_required
    @classmethod
    def get(cls, request):
        if request.user.is_authenticated():
            # 做一些事情针对验证用户.
            return JsonResponse({'code': '1'})
        else:
            user = authenticate(username='cqhgb-1', password='hgbyx-20191118')
            if user.is_authenticated:
                login(request, user)
            return JsonResponse({'code': '0'})

    @classmethod
    def post(cls, request):
        ret = eval(request.body.decode())
        start = datetime.strptime(ret['start'], "%Y-%m-%d")
        end = datetime.strptime(ret['end'], "%Y-%m-%d")
        errlog.info(start)
        errlog.info(end)
        if start > end:
            errlog.info(1)
        else:
            errlog.info(2)
        return JsonResponse({'code': "text"})


# @accept_websocket
# @require_websocket

# @login_required
@accept_websocket
def websocket(request):
    # if(i=)
    if request.is_websocket():
        while True:
            web_obj = request.websocket
            message = web_obj.wait().decode()
            web_obj.send(message)
            time.sleep(10)
    con = {
        'code': 'yes'
    }
    return JsonResponse(con)


def loginview(request):
    return HttpResponseRedirect('/login.html')



def isthererecord(request):
    return JsonResponse({'code': '0'})
